package cn.com.pajx.pajx_visitor_v3.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

/**
 * created by ruancw on 2024/2/28.
 * 日期工具类
 */
public class DateUtil {
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_FORMAT_HM = "HH:mm";
    public static final String DATE_FORMAT_NYDHMS = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT_TEXT = "MM月dd日";
    private static final String DATE_FORMAT_SECOND = "yyyy-MM-dd HH:mm";
    private static final String DATE_FORMAT_NOTICE = "MM-dd HH:mm";
    private static final String DATE_FORMAT_ATT = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_YMD = "yyyyMMdd";
    private static final String DATE_FORMAT_CHINESE = "yyyy年MM月dd日 HH:mm";

    private static boolean checkFormatData(String seconds) {
        return !TextUtils.isEmpty(seconds) && TextUtils.isDigitsOnly(seconds) && seconds.length() >= 10;
    }

    public static String getFormatData(String seconds) {
        return getFormatData(seconds, DATE_FORMAT);
    }

    public static String getFormatData(String seconds, String formatStr) {
        if (checkFormatData(seconds)) {
            SimpleDateFormat sdf = new SimpleDateFormat(formatStr, Locale.getDefault());
            return sdf.format(new Date(Long.parseLong(seconds + "000")));
        }
        return "";

    }

    //1499074683-> 10:10  / 2017-02-02
    public static String getMainLastTime(String time) {
        if (TextUtils.isEmpty(time) || !TextUtils.isDigitsOnly(time) || time.length() != 10) {
            return "";
        }
        if (android.text.format.DateUtils.isToday(Long.parseLong(time))) {
            return getFormatData(time, DATE_FORMAT_HM);
        }
        return getFormatData(time);
    }

    public static String getMainNoticeTime(String time) {
        if (TextUtils.isEmpty(time) || !TextUtils.isDigitsOnly(time) || time.length() != 10) {
            return "";
        }
        if (android.text.format.DateUtils.isToday(Long.parseLong(time))) {
            return getFormatData(time, DATE_FORMAT_HM);
        }
//        return getFormatData(time, DATE_FORMAT_NOTICE);
        return getFormatData(time, DATE_FORMAT_SECOND);
    }

    public static String getCircleTime(String time) {
        if (TextUtils.isEmpty(time) || !TextUtils.isDigitsOnly(time) || time.length() != 10) {
            return "";
        }
        if (android.text.format.DateUtils.isToday(Long.parseLong(time))) {
            return getFormatData(time, DATE_FORMAT_HM);
        }
//        return getFormatData(time, DATE_FORMAT_NOTICE);
        return getFormatData(time, DATE_FORMAT_CHINESE);
    }

    public static String getCurrentYm() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM", Locale.getDefault());
        return sdf.format(new Date(System.currentTimeMillis()));
    }

    public static String getCurrentYmd() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_YMD, Locale.getDefault());
        return sdf.format(new Date(System.currentTimeMillis()));
    }

    public static String getCurrentTime() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_ATT, Locale.getDefault());
        return sdf.format(new Date(System.currentTimeMillis()));
    }

    public static String getCurrentYMD() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault());
        return sdf.format(new Date(System.currentTimeMillis()));
    }

    public static String getCurrentYMDHMS() {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NYDHMS, Locale.getDefault());
        return sdf.format(new Date(System.currentTimeMillis()));
    }

    public static String getDateFormatText(String seconds) {
        if (TextUtils.isEmpty(seconds) || !TextUtils.isDigitsOnly(seconds) || seconds.length() < 10) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_TEXT, Locale.getDefault());
        return sdf.format(new Date(Long.parseLong(seconds + "000")));
    }

    public static String getSecendDate(String seconds) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_SECOND, Locale.getDefault());
        return sdf.format(new Date(Long.parseLong(seconds + "000")));
    }

    /*========考勤=========*/
//    20170202120202->2017-02-02 12:02:02
    public static String getAllTime(String time) {
        if (TextUtils.isEmpty(time) || !TextUtils.isDigitsOnly(time) || time.length() != 14) {
            return "";
        }
        return time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(8, 10)+":"+time.substring(10, 12)+":"+time.substring(12, 14);
    }

    public static String getAttDetailTime(String lastTime) {
        if (TextUtils.isEmpty(lastTime) || !TextUtils.isDigitsOnly(lastTime) || lastTime.length() != 14) {
            return "";
        }
        return lastTime.substring(8, 10) + ":" + lastTime.substring(10, 12) + ":" + lastTime.substring(12, 14);
    }

    public static String getYMDTime(String lastTime) {
        if (TextUtils.isEmpty(lastTime) || !TextUtils.isDigitsOnly(lastTime) || lastTime.length() != 14) {
            return "";
        }
        return lastTime.substring(0, 4) + "-" + lastTime.substring(4, 6) + "-" + lastTime.substring(6, 8);
    }

    public static String getYMDHMTime(String time) {
        if (TextUtils.isEmpty(time) || !TextUtils.isDigitsOnly(time) || time.length() != 14) {
            return "";
        }
        return time.substring(0, 4) + "-" + time.substring(4, 6) + "-" + time.substring(6, 8) + " " + time.substring(8, 10)+":"+time.substring(10, 12);
    }

    public static String getMDHMTime(String time) {
        if (TextUtils.isEmpty(time) || !TextUtils.isDigitsOnly(time) || time.length() != 14) {
            return "";
        }
        return time.substring(4, 6) + "." + time.substring(6, 8) + " " + time.substring(8, 10)+":"+time.substring(10, 12);
    }

    public static String getMDTime(String lastTime) {
        if (TextUtils.isEmpty(lastTime) || !TextUtils.isDigitsOnly(lastTime) || lastTime.length() != 14) {
            return "";
        }
        return lastTime.substring(4, 6) + "/" + lastTime.substring(6, 8) + " " + lastTime.substring(8, 10)+ ":" + lastTime.substring(10, 12);
    }

    public static long date2TimeStamp(String date, String format) {
        try {
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return Objects.requireNonNull(sdf.parse(date)).getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getWeekTime(String lastTime) {
        if (TextUtils.isEmpty(lastTime) || !TextUtils.isDigitsOnly(lastTime) || lastTime.length() != 14) {
            return "";
        }
        return lastTime.substring(0, 4) + "-" +lastTime.substring(4, 6) + "-" + lastTime.substring(6, 8);
    }

    /**
     * 时间转换（秒转换成时分秒）
     *
     * @param time time
     * @return 字符串
     */
    public static String secToTime(int time) {
        String timeStr;
        int hour;
        int minute;
        int second;
        if (time <= 0)
            return "0秒";
        else {
            minute = time / 60;
            if (minute == 0) {
                second = time % 60;
                timeStr = second + "秒";
            } else if (minute < 60) {
                second = time % 60;
                timeStr = minute + "分" + unitFormat(second) + "秒";
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99时59分59秒";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = hour + "时" + unitFormat(minute) + "分" + unitFormat(second);
            }
        }
        return timeStr;
    }

    private static String unitFormat(int i) {
        String retStr;
        if (i >= 0 && i < 10)
            retStr = "0" + i;
        else
            retStr = "" + i;
        return retStr;
    }

    // 将传入时间与当前时间进行对比，是否今天\昨天\前天\同一年
    @SuppressLint("SimpleDateFormat")
    public static String getTime(String str) {
        Date date = new Date(Long.parseLong(str + "000"));
        boolean sameYear;
        String yesterDaySDF = "昨天";
        String beforYesterDaySDF = "前天";
//        String otherSDF = "MM-dd";
//        String otherYearSDF = "yyyy-MM-dd";
        SimpleDateFormat sfd;
        String time;
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        Date now = new Date();
        Calendar todayCalendar = Calendar.getInstance();
        todayCalendar.setTime(now);
        todayCalendar.set(Calendar.HOUR_OF_DAY, 0);
        todayCalendar.set(Calendar.MINUTE, 0);

        sameYear = dateCalendar.get(Calendar.YEAR) == todayCalendar.get(Calendar.YEAR);

        if (dateCalendar.after(todayCalendar)) {// 判断是不是今天
            sfd = new SimpleDateFormat(DATE_FORMAT_HM);
            time = sfd.format(date);
            return time;
        } else {
            todayCalendar.add(Calendar.DATE, -1);
            if (dateCalendar.after(todayCalendar)) {// 判断是不是昨天
                // sfd = new SimpleDateFormat(yesterDaySDF);
                // time = sfd.format(date);
                time = yesterDaySDF;
                return time;
            }
            todayCalendar.add(Calendar.DATE, -2);
            if (dateCalendar.after(todayCalendar)) {// 判断是不是前天
                // sfd = new SimpleDateFormat(beforYesterDaySDF);
                // time = sfd.format(date);
                time = beforYesterDaySDF;
                return time;
            }
        }

        if (sameYear) {
            sfd = new SimpleDateFormat(DATE_FORMAT_NOTICE);
        } else {
            sfd = new SimpleDateFormat(DATE_FORMAT_SECOND);
        }
        time = sfd.format(date);

        return time;
    }

    public static String getAttendanceDate(String lastTime) {
        if (TextUtils.isEmpty(lastTime) || !TextUtils.isDigitsOnly(lastTime) || lastTime.length() != 14) {
            return "";
        }
        return lastTime.substring(0, 4) + "-" + lastTime.substring(4, 6) + "-" + lastTime.substring(6, 8) + " \n" + lastTime.substring(8, 10) + ":" + lastTime.substring(10, 12) + ":" + lastTime.substring(12, 14);
    }

    public static String getWeekAndDateByService(String attTime) {

        //规定yyyy-MM-dd这种格式
        @SuppressLint("SimpleDateFormat")
        DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        String week = null;
        //传入一个字符串，将字符串转换成date类型
        try {
            Date date = df.parse(getWeekTime(attTime));
            //“E”指定星期几这样的格式
            @SuppressLint("SimpleDateFormat")
            SimpleDateFormat s1=new SimpleDateFormat("E");
            //将date转换成String
            assert date != null;
            week = s1.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return week + " " + getAttDetailTime(attTime);
    }

    public static String getMSByServiceDate(Context c, String serviceDate) {
        if (!TextUtils.isEmpty(serviceDate) && serviceDate.length() == 14) {
            try {
                String year = serviceDate.substring(0, 4);
                String month = serviceDate.substring(4, 6);
                String day = serviceDate.substring(6, 8);
                String hour = serviceDate.substring(8, 10);
                String m = serviceDate.substring(10, 12);
                String s = serviceDate.substring(12, 14);
                return hour + ":" + m;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    //获取周几
    // 1-周7 2-周1 3-周2 4-周3 5-周4 6-周5 7-周6
    public static int getDayofweek(String date) {
        Calendar cal = Calendar.getInstance();
        if (TextUtils.equals(date, "")) {
            cal.setTime(new Date(System.currentTimeMillis()));
        } else {
            cal.setTime(new Date(getDateByStr2(date).getTime()));
        }
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    public static Date getDateByStr2(String dd) {
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Date date;
        try {
            date = sd.parse(dd);
        } catch (ParseException e) {
            date = null;
            e.printStackTrace();
        }
        return date;
    }

    //1-12转汉字
    public static String week2hz(int week) {
        String str = "";
        if (week > 7 || week < 0) {
            return str;
        }
        switch (week) {
            case 1:
                return "一";
            case 2:
                return "二";
            case 3:
                return "三";
            case 4:
                return "四";
            case 5:
                return "五";
            case 6:
                return "六";
            case 7:
                return "日";
        }
        return str;
    }

    public static String formatSeconds(long seconds){
        String standardTime;
        if (seconds <= 0){
            standardTime = "00:00:00";
        } else if (seconds < 60) {
            standardTime = String.format(Locale.getDefault(), "00:00:%02d", seconds % 60);
        } else if (seconds < 3600) {
            standardTime = String.format(Locale.getDefault(), "00:%02d:%02d", seconds / 60, seconds % 60);
        } else {
            standardTime = String.format(Locale.getDefault(), "%02d:%02d:%02d", seconds / 3600, seconds % 3600 / 60, seconds % 60);
        }
        return standardTime;
    }


    public static String getPreviousDay(String currentDate) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            Date date = sdf.parse(currentDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -1); // 减去一天
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getNextDay(String currentDate) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        try {
            Date date = sdf.parse(currentDate);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, 1); // 加一天
            return sdf.format(calendar.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
}